﻿using Com.Qiniu.ShopErp.App.Device.Print;
using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using Mengsk.ProxyDelivery.Utls;
using Seagull.BarTender.Print;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Com.Qiniu.ShopErp.App.Device.Print
{
    class PrintDevice
    {
        public static string template = System.IO.Path.Combine(EnvironmentDirHelper.QINIU_SHOPERP_DIR_DATA, @"Barcode\OrderChangeTemplate.btw");

        public static void PrintOrderReturn(params OrderChange[] ors)
        {
            var prs = new OrderReturnPrintInfo[ors.Length];
            for (int i = 0; i < ors.Length; i++)
            {
                int id = ors[i].Id;
                string goodsInfo = ors[i].GoodsInfo;
                string[] items = goodsInfo.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
                var gi = new OrderReturnPrintInfo();
                gi.Id = id.ToString();
                gi.Vendor = VendorService.FormatVendorName(items[0]);
                string stocking = "";
                for (int j = 1; j < items.Length; j++)
                {
                    stocking += items[j];
                }
                gi.Stocking = stocking;

                var vendors = ServiceContainer.GetService<VendorService>().GetAllByField("Name", items[0], 0, 0);
                if (vendors == null || vendors.Total < 1)
                {
                    MessageBox.Show("未找到厂家:" + goodsInfo, "未打印信息", MessageBoxButton.OK, MessageBoxImage.Error);
                    continue;
                }
                if (vendors.Total > 1)
                {
                    MessageBox.Show("找到多个厂家:" + goodsInfo, "未打印信息", MessageBoxButton.OK, MessageBoxImage.Error);
                    continue;
                }
                gi.Door = VendorService.FormatVendorDoor(vendors.Datas[0].MarketAddress);
                prs[i] = gi;
            }
            PrintDevice.Print(prs);
        }

        public static void Print(params OrderReturnPrintInfo[] goodsInfo)
        {
            string printerName = LocalConfigService.GetValue(LocalConfigService.CONFIG_HOTPRINT, "");
            if (string.IsNullOrWhiteSpace(printerName))
            {
                MessageBox.Show("无法打印，请配置打印机名称");
                return;
            }

            if (System.IO.File.Exists(template) == false)
            {
                throw new Exception("打印模板文件不存在:" + template);
            }


            using (Engine en = new Engine())
            {
                try
                {
                    en.Start();
                    LabelFormatDocument doc = en.Documents.Open(template);
                    doc.PrintSetup.PrinterName = printerName;
                    foreach (var item in goodsInfo)
                    {
                        doc.SubStrings["OrderChangeId"].Value = item.Id.ToString();
                        doc.SubStrings["VendorName"].Value = item.Vendor;
                        doc.SubStrings["VendorDoor"].Value = item.Door;
                        doc.SubStrings["Stocking"].Value = item.Stocking;

                        Messages m = null;
                        var ret = doc.Print(item.Id, out m);
                        if (ret != Result.Success)
                        {
                            throw new Exception("打印失败:" + String.Join(Environment.NewLine, m.Select(obj => obj.Category + "," + obj.ID + "," + obj.Text)));
                        }
                    }
                }
                finally
                {
                    en.Stop();
                }
            }
        }
    }
}
